package com.zqp.web.anync;

import java.util.concurrent.Callable;

import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

/**
* @author zqp
* @date 2017年12月19日 
* @description 
*/
@RestController
public class AsyncController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private MockQueue mockQueue;
	@Autowired
	private DeferredResultHolder deferredResultHolder;
	@GetMapping("/order")
	public DeferredResult<String> order() throws Exception {
		logger.info("主线程开始");
		String  orderNumer = RandomStringUtils.randomNumeric(8);
		
		mockQueue.setPlaceOrder(orderNumer);
		
		DeferredResult<String> result = new DeferredResult<>();
		
		deferredResultHolder.getMap().put(orderNumer, result);
//		//单开一个线程
//		Callable<String> callable = new Callable<String>() {
//
//			@Override
//			public String call() throws Exception {
//				logger.info("副线程开始");	
//				Thread.sleep(1000); 
//				logger.info("副线程返回");	
//				return "succes";
//			}
//		};
		
		
//	
		logger.info("主线程返回");
		return result;
	}
	
	
	
	
	
	/**
	 * 使用 callable  单开一个线程
	 */
//	@GetMapping("/order")
//	public Callable<String> order() throws Exception {
//		logger.info("主线程开始");
//		//单开一个线程
//		Callable<String> callable = new Callable<String>() {
//
//			@Override
//			public String call() throws Exception {
//				logger.info("副线程开始");	
//				Thread.sleep(1000); 
//				logger.info("副线程返回");	
//				return "succes";
//			}
//		};
//		
//		logger.info("主线程返回");
//		return callable;
//	}
	
}
